/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package crypto;


import java.sql.SQLException;
import java.util.Calendar;
import java.util.ArrayList;
import java.sql.ResultSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.lang.String;
import java.util.HashMap;
import java.util.Map;
import java.util.Arrays;
import java.lang.Comparable;
/**
 *
 * @author Cecilia Bartley, Isaac Osesina
 */

public class Results2 {

    // takes all words from a file, scrambles them and puts the output into the set called newSet
    
public Set<String> ScrambleEverything (Set<String> finaloutput){
    Set<String> ScrambleRecursion = null;
    Set<String> newSet = new HashSet<String>();
    Word_Match wordmatchtest = new Word_Match();
    
    for (String element:finaloutput)//for every element of the string set
    {
            ScrambleRecursion = wordmatchtest.ScrambleRecursion(element);//use the scramble recursion method

            newSet.addAll(ScrambleRecursion);//add the new word to the new set called newSet
    }
    return newSet; //return the new set called newSet
}


    //checks each element line by line from a set against 1 line of text

public Set<String> TextWordCheck (String text, Set<String> word){
    Set<String> newSet2 = new HashSet<String>(); //newSet2 object
    Word_Match wordmatchtest = new Word_Match();  //new Word_Match object called wordmatchtest
    
      for (String element2:word)
         {
             boolean res = wordmatchtest.cc(text,element2);

             if(res == true)
             
                newSet2.add(element2);

         }
         return newSet2;         
           
}
//Return a map of type string and integer array list the following method whose parameters are (string txt and string set called search)
public Map<String,ArrayList<Integer>> SearchandIndex (String text, Set<String> search){
    Map<String,ArrayList<Integer>> newSet3 = new HashMap();  //Creates a new hashmap object called newSet3
    Word_Match wordmatchtest = new Word_Match();  //Creates a new object call

    for (String element3:search) //for each element within the set
    {
            ArrayList<Integer> cc2 = wordmatchtest.cc2(text, element3);  //Array list of type integer called cc2 equals 

            if(cc2 != null && !cc2.isEmpty())  //Nested loop, that if the array list is not of type null or empty, then add

                newSet3.put(element3, cc2);

    }
        return newSet3;
}


//IO-3: this is a way to go through each tweet and check it against the target words
public Map checkAllTweets(ArrayList<String> tweets, HashSet<String> targetwords) {
    Map<String, Map<String, ArrayList<Integer>>> newMap = new HashMap<String, Map<String, ArrayList<Integer>>>();

    //Scramble all target words
    Set<String> everythingAlreadyScrambled = ScrambleEverything(targetwords);

    //get each of my tweet and check it against scrambled target words
    for(String tweet:tweets) {

            //identify scarambled target words contained in text
            Set<String> foundScrambledWords = TextWordCheck(tweet, everythingAlreadyScrambled);
            //get the start position of dound scarambled words
            Map<String, ArrayList<Integer>> startPositionOfFoundWords = SearchandIndex(tweet, foundScrambledWords);
            //save found words and their position for this tweet text
            newMap.put(tweet, startPositionOfFoundWords);
    }

    return newMap;
}

public Map MM (ArrayList<String> tweets, HashSet<String> targetwords){
//get tweets and target words, scramble the target words, check them against the arraylist of tweets and save the results in a map
   
    Map<String, Map<String, ArrayList<Integer>>> newMap = new HashMap<String, Map<String, ArrayList<Integer>>>();

    Set<String> EverythingAlreadyScrambled = ScrambleEverything(targetwords);  //Scrambles all target words

        for (String tweet:tweets){
            Set<String> FoundScrambledWords = TextWordCheck(tweet,EverythingAlreadyScrambled);  //Identifies scrambled target words contained in the tweets
            Map<String, ArrayList<Integer>> StartPositionofFoundWords = SearchandIndex(tweet, FoundScrambledWords);  //Gives the start position of the found target words within the tweets
            newMap.put(tweet, StartPositionofFoundWords);
       }
   
return newMap;
}


public static void main(String [] args) {
      //Read target words, read db tweets

      Set<String> TG = null;
      Set<String> targetwords = TG;
      ArrayList<String> TT = null;
      ArrayList<String>  tweets = TT;


      FileRead fr = new FileRead();  //Creates a new file read object to read the target word file
      Set main = fr.main(args);

      DBQuery dbquerytest = new DBQuery("jdbc:mysql://144.167.34.111:3306/", "twitterdatacopy", "cecilia", "cecilia123", Calendar.getInstance());
      List<Tweet> queryTweetTextAndLocation = dbquerytest.queryTweetTextAndLocation(null);

      

      Results2 r = new Results2();
      Map<Set<String>,ArrayList<String>> MM = r.MM(TT, (HashSet<String>) TG);
        
      
System.out.println(MM);
    
    }

}
